//
//  ChessboardView.m
//  gobang
//
//  Created by huangyu on 11-6-13.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "ChessboardView.h"


@implementation ChessboardView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        [self clearChessFlag];
        chessTurn = WHITE_CHESS;
        status = CHESS_START;
    }
    return self;
}


- (void)dealloc
{
    [super dealloc];
}

- (void)clearChessFlag{
    memset(chessMap, 0, sizeof(int)*ROW_COUNT*COLUMN_COUNT);
}

- (void)setChessFlag:(int)flag withRow:(int)row withColumn:(int)column{
    chessMap[row][column] = flag;
}


- (int)judgeSuccess:(int)flag withRow:(int)row withColumn:(int)column{
    int count = 0;
    // |
    for (int i = row; i >= 0; i--) {
        if (chessMap[i][column]==flag) {
            count++;
        }else{
            break;
        }
            
    }
    
    for (int i = row+1; i < ROW_COUNT; i++) {
        if (chessMap[i][column]==flag) {
            count++;
        }else{
            break;
        }
            
    }
    if (count >= 5) {
        return flag;
    }
    
    // -
    count = 0;
    for (int i = column; i >= 0; i--) {
        if (chessMap[row][i]==flag) {
            count++;
        }else{
            break;
        }
        
    }
    
    for (int i = column+1; i < COLUMN_COUNT; i++) {
        if (chessMap[row][i]==flag) {
            count++;
        }else{
            break;
        }
        
    }
    if (count >= 5) {
        return flag;
    }
    
    count = 0;
    for (int i = row,j=column; i >= 0&&j < COLUMN_COUNT; i--,j++) {
        if (chessMap[i][j]==flag) {
            count++;
        }else{
            break;
        }
        
    }
    
    for (int i = row+1,j=column-1; i < ROW_COUNT&& j >=0; i++,j--) {
        if (chessMap[i][j]==flag) {
            count++;
        }else{
            break;
        }
        
    }
    if (count >= 5) {
        return flag;
    }
    
    
    
    
    return 0;
}


- (void)drawBackground {
	// Draw a rectangle fill with color from skins
	CGContextRef context = UIGraphicsGetCurrentContext();
	[[UIColor colorWithRed:241/255.0 green:235/255.0 blue:222/255.0 alpha:1.0] set];
    
    CGContextFillRect(context, CGRectMake(0, 0, 320, 480));
    
    
	[[UIColor blackColor] set];
    int startx = 0.0;
	int starty = 16.0;
	// Draw horizontal line
    for (int i = 0; i < ROW_COUNT + 2; i++)
        CGContextFillRect(context, CGRectMake(startx, starty+i*32.0, 320.0, 1.0));
    
	// Draw vertical line
    startx = 0;
    starty = 16.0;
    for (int i = 0; i < COLUMN_COUNT + 2; i++)
        CGContextFillRect(context, CGRectMake(startx+i*32.0, starty, 1.0, 384.0));
}

- (void)drawChess {
    CGContextRef context = UIGraphicsGetCurrentContext();
    for (int i = 0; i < ROW_COUNT; i++) {
        for (int j = 0; j < COLUMN_COUNT; j++) {
            if (chessMap[i][j]==WHITE_CHESS) {
                [[UIColor grayColor] setStroke];
                [[UIColor whiteColor] setFill];
                CGContextStrokeEllipseInRect(context, CGRectMake(16+32*j, 32+32*i, 30, 30));
                CGContextFillEllipseInRect(context, CGRectMake(16+32*j, 32+32*i, 30, 30));                
            }else if(chessMap[i][j]==BLACK_CHESS){
                [[UIColor blackColor] set];
                CGContextFillEllipseInRect(context, CGRectMake(16+32*j, 32+32*i, 30, 30));
            }
        }
    }
}

- (void)drawRect:(CGRect)rect{
    [self drawBackground];    
    [self drawChess];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    
    if (status != CHESS_START) {
        return;
    }
    
    UITouch *touch = [touches anyObject];
    CGPoint touchPoint = [touch locationInView:self];
    
    if (touchPoint.x > 16 && touchPoint.y > 32 && touchPoint.x < 304 && touchPoint.y < 384) {
        int column = (touchPoint.x - 16)/32;
        int row = (touchPoint.y - 32)/32;
        if (chessMap[row][column] == 0) {
            [self setChessFlag:chessTurn withRow:row withColumn:column];
            [self setNeedsDisplay];
            
            if([self judgeSuccess:chessTurn withRow:row withColumn:column] == chessTurn){
                NSString *msg;
                if (chessTurn == WHITE_CHESS) {
                    msg = @"白子胜利!";
                }else{
                    msg = @"黑子胜利!";
                }
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:msg delegate:nil cancelButtonTitle:@"ok" otherButtonTitles: nil];
                [alert show];
                [alert release];
                status = CHESS_STOP;
            }
            chessTurn = -chessTurn;
        }
    }
}

- (void)restartChess{
    
    [self clearChessFlag];
    status = CHESS_START;
    [self setNeedsDisplay];
}

@end
